package com.lmx.repoDomain.tmodel.business;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.lmx.repoDomain.tmodel.BaseEntity;
import com.lmx.repoDomain.tmodel.Customer;
import com.lmx.repoDomain.tmodel.Dicttype;
import com.lmx.repoDomain.tmodel.Employee;
import com.lmx.repoDomain.tmodel.Privilege;
import com.lmx.repoDomain.tmodel.Role;

import io.katharsis.resource.annotations.JsonApiIncludeByDefault;
import io.katharsis.resource.annotations.JsonApiLookupIncludeAutomatically;
import io.katharsis.resource.annotations.JsonApiResource;
import io.katharsis.resource.annotations.JsonApiToMany;
import io.katharsis.resource.annotations.JsonApiToOne;

/**
 * 系统用户表
 */
@Entity
@JsonApiResource(type = "user")
@Table(name = "t_s_base_user")
// @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
// @JsonIgnoreProperties(value={"hibernateLazyInitializer","handler"})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User extends BaseEntity {

	private String loginName;// 用户名
	protected Role role;// 角色
	private String name;// 真实姓名
	private String entName;// 企业名称
	private String password;// 用户密码
	private String avatar;// 头像文件名
	private String desc;// 自我描述
	private Short onlineStatus;// 在线状态 0：离线 1：在线
	private Short status;// 状态 0：禁用 1：未认证 2：已认证
	// private Short sex;// 性别,1男2女
	// private String phone;// 电话号码
	// private String email;// 邮箱
	private String weixin;// 微信号
	private String qqnum;// QQ
	private Integer age;// 年龄
	// private Set<Dicttype> cstag;// 客服标签表
	// private Set<Dicttype> language;// 语言
	private String introduce;// 简介
	// private Userextend extendInfo;// 用户扩展
	// private Edinfo edinfo;// 企业信息
	// private UserAgent agentinfo;// 坐席信息信息-华为
	private String regWay;// 注册途径
	private String verifyFailReason;// 审核失败原因
	private String inviter;// 邀请人
	private User inviteUser;// 邀请人
	private List<User> invitees;// 被邀请人
	// private Integer createTime;// 创建时间
	// private Integer updateTime; //更新时间
	private String remark; // 备注
	private Organization org; // 备注
	private String authCode;// 验证码
	private String passcode;// 登录密码

	private String appPassWord;// app登录密码

	private String certificateImage;// 证书图片

	private Integer createDateTime;// 创建时间

	private User createUser;// 创建者id

	private String curAddress;// 当前地址

	// private Department dept;// 需关联部门表

	private String hbeaconID; // 手环id

	private Dicttype hireType;// 雇佣类型

	private Short isGroupFlag;// 是否为护理组成员

	private Short isNurseFlag;// 是否为护工

	private Integer lastUpdateDateTime;// 更新时间

	private User lastUpdateUser;// 更新者id

	private String profession;// 专业

	private String schoolName;// 学校名称

	private Integer staffBirth;// 生日

	private String staffTel;

	private String staffCardCode;// 身份证

	private Dicttype staffCensus;// 户籍

	private String certificateName;// 证书名称

	private String staffContactName;// 联系人名字

	private Dicttype staffContactRelation;// 联系人关系

	private String staffContactTel;// 联系人手机

	private Dicttype staffEducation;// 学历

	private String staffMail;// 邮箱

	private Dicttype staffMaritalStatus;// 婚姻状态

	// private String staffName;// 名称

	private Dicttype staffNation;// 民族

	private Dicttype staffSex;// 性别

	private Dicttype staffStatus;// 职工状态

	private Integer toPositionDate;// 到岗时间

	// private String showName;// 登录名

	private List<Customer> customers;// 所关注的老人

	private Employee employee;// 员工

	private String openId;

	@Column(name = "openid")
	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	@JsonApiToOne
	@JsonApiLookupIncludeAutomatically(overwrite = true)
	// @JsonApiIncludeByDefault
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.JOIN)
	@JoinColumn(name = "employee_id", foreignKey = @ForeignKey(name = "null"))
	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	@JsonApiToMany
	@ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
	@JoinTable(name = "tnb_staff_customer", joinColumns = { @JoinColumn(name = "staff_id", nullable = false, updatable = false, foreignKey = @ForeignKey(name = "null")) }, inverseJoinColumns = { @JoinColumn(name = "customer_id", nullable = false, updatable = false, foreignKey = @ForeignKey(name = "null")) })
	@Fetch(FetchMode.JOIN)
	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	// private List<StaffCustomer> customers;// 护工所护理的老人
	//
	// private List<StaffCustomer> clientCustomers;// client所关注的老人
	//
	// @JsonApiToMany
	// @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH,
	// mappedBy = "staff")
	// @Where(clause = "staff.role.type=3")
	// public List<StaffCustomer> getClientCustomers() {
	// return clientCustomers;
	// }
	//
	// public void setClientCustomers(List<StaffCustomer> clientCustomers) {
	// this.clientCustomers = clientCustomers;
	// }
	//
	// @JsonApiToMany
	// @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH,
	// mappedBy = "staff")
	// @Where(clause = "staff.role.type=2")
	// public List<StaffCustomer> getCustomers() {
	// return customers;
	// }
	//
	// public void setCustomers(List<StaffCustomer> customers) {
	// this.customers = customers;
	// }

	@Column(name = "passcode")
	public String getPasscode() {
		return passcode;
	}

	public void setPasscode(String passcode) {
		this.passcode = passcode;
	}

	private Privilege privilege;// 权限

	// private String sysPassWord;// 系统登录密码

	@Column(name = "staffTel")
	public String getStaffTel() {
		return staffTel;
	}

	public void setStaffTel(String staffTel) {
		this.staffTel = staffTel;
	}

	@Column(name = "appPassWord")
	public String getAppPassWord() {
		return appPassWord;
	}

	public void setAppPassWord(String appPassWord) {
		this.appPassWord = appPassWord;
	}

	@Column(name = "certificateImage")
	public String getCertificateImage() {
		return certificateImage;
	}

	public void setCertificateImage(String certificateImage) {
		this.certificateImage = certificateImage;
	}

	@Column(name = "createDateTime")
	public Integer getCreateDateTime() {
		return createDateTime;
	}

	public void setCreateDateTime(Integer createDateTime) {
		this.createDateTime = createDateTime;
	}

	@JsonApiToOne
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	@JoinColumn(name = "createUser_id", foreignKey = @ForeignKey(name = "null"))
	public User getCreateUser() {
		return createUser;
	}

	public void setCreateUser(User createUser) {
		this.createUser = createUser;
	}

	@Column(name = "hbeaconID")
	public String getHbeaconID() {
		return hbeaconID;
	}

	public void setHbeaconID(String hbeaconID) {
		this.hbeaconID = hbeaconID;
	}

	@JsonApiToOne
	@JsonApiIncludeByDefault
	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.SELECT)
	// huibati
	@JoinColumn(name = "hireType_id", foreignKey = @ForeignKey(name = "null"))
	public Dicttype getHireType() {
		return hireType;
	}

	public void setHireType(Dicttype hireType) {
		this.hireType = hireType;
	}

	@Column(name = "isGroupFlag")
	public Short getIsGroupFlag() {
		return isGroupFlag;
	}

	public void setIsGroupFlag(Short isGroupFlag) {
		if (isGroupFlag == null)
			isGroupFlag = 0;
		this.isGroupFlag = isGroupFlag;
	}

	@Column(name = "isNurseFlag")
	public Short getIsNurseFlag() {
		return isNurseFlag;
	}

	public void setIsNurseFlag(Short isNurseFlag) {
		if (isNurseFlag == null)
			isNurseFlag = 0;
		this.isNurseFlag = isNurseFlag;
	}

	@Column(name = "lastUpdateDateTime")
	public Integer getLastUpdateDateTime() {
		return lastUpdateDateTime;
	}

	public void setLastUpdateDateTime(Integer lastUpdateDateTime) {
		this.lastUpdateDateTime = lastUpdateDateTime;
	}

	@JsonApiToOne
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	@JoinColumn(name = "lastUpdateUser_id", foreignKey = @ForeignKey(name = "null"))
	public User getLastUpdateUser() {
		return lastUpdateUser;
	}

	public void setLastUpdateUser(User lastUpdateUser) {
		this.lastUpdateUser = lastUpdateUser;
	}

	@Column(name = "profession")
	public String getProfession() {
		return profession;
	}

	public void setProfession(String profession) {
		this.profession = profession;
	}

	@Column(name = "schoolName")
	public String getSchoolName() {
		return schoolName;
	}

	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;
	}

	@Column(name = "staffBirth")
	public Integer getStaffBirth() {
		return staffBirth;
	}

	public void setStaffBirth(Integer staffBirth) {
		this.staffBirth = staffBirth;
	}

	@Column(name = "staffCardCode")
	public String getStaffCardCode() {
		return staffCardCode;
	}

	public void setStaffCardCode(String staffCardCode) {
		this.staffCardCode = staffCardCode;
	}

	@JsonApiToOne
	@JsonApiIncludeByDefault
	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.SELECT)
	// huibati
	@JoinColumn(name = "staffCensus_id", foreignKey = @ForeignKey(name = "null"))
	public Dicttype getStaffCensus() {
		return staffCensus;
	}

	public void setStaffCensus(Dicttype staffCensus) {
		this.staffCensus = staffCensus;
	}

	@Column(name = "certificateName")
	public String getCertificateName() {
		return certificateName;
	}

	public void setCertificateName(String certificateName) {
		this.certificateName = certificateName;
	}

	@Column(name = "staffContactName")
	public String getStaffContactName() {
		return staffContactName;
	}

	public void setStaffContactName(String staffContactName) {
		this.staffContactName = staffContactName;
	}

	@JsonApiToOne
	@JsonApiIncludeByDefault
	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.SELECT)
	// huibati
	@JoinColumn(name = "staffContactRelation_id", foreignKey = @ForeignKey(name = "null"))
	public Dicttype getStaffContactRelation() {
		return staffContactRelation;
	}

	public void setStaffContactRelation(Dicttype staffContactRelation) {
		this.staffContactRelation = staffContactRelation;
	}

	@Column(name = "staffContactTel")
	public String getStaffContactTel() {
		return staffContactTel;
	}

	public void setStaffContactTel(String staffContactTel) {
		this.staffContactTel = staffContactTel;
	}

	@JsonApiToOne
	@JsonApiIncludeByDefault
	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.SELECT)
	// huibati
	@JoinColumn(name = "staffEducation_id", foreignKey = @ForeignKey(name = "null"))
	public Dicttype getStaffEducation() {
		return staffEducation;
	}

	public void setStaffEducation(Dicttype staffEducation) {
		this.staffEducation = staffEducation;
	}

	@Column(name = "staffMail")
	public String getStaffMail() {
		return staffMail;
	}

	public void setStaffMail(String staffMail) {
		this.staffMail = staffMail;
	}

	@JsonApiToOne
	@JsonApiIncludeByDefault
	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.SELECT)
	// huibati
	@JoinColumn(name = "staffMaritalStatus_id", foreignKey = @ForeignKey(name = "null"))
	public Dicttype getStaffMaritalStatus() {
		return staffMaritalStatus;
	}

	public void setStaffMaritalStatus(Dicttype staffMaritalStatus) {
		this.staffMaritalStatus = staffMaritalStatus;
	}

	@JsonApiToOne
	@JsonApiIncludeByDefault
	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.SELECT)
	// huibati
	@JoinColumn(name = "staffNation_id", foreignKey = @ForeignKey(name = "null"))
	public Dicttype getStaffNation() {
		return staffNation;
	}

	public void setStaffNation(Dicttype staffNation) {
		this.staffNation = staffNation;
	}

	@JsonApiToOne
	@JsonApiIncludeByDefault
	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.SELECT)
	// huibati
	@JoinColumn(name = "staffSex_id", foreignKey = @ForeignKey(name = "null"))
	public Dicttype getStaffSex() {
		return staffSex;
	}

	public void setStaffSex(Dicttype staffSex) {
		this.staffSex = staffSex;
	}

	@JsonApiToOne
	@JsonApiIncludeByDefault
	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.SELECT)
	// huibati
	@JoinColumn(name = "staffStatus_id", foreignKey = @ForeignKey(name = "null"))
	public Dicttype getStaffStatus() {
		return staffStatus;
	}

	public void setStaffStatus(Dicttype staffStatus) {
		this.staffStatus = staffStatus;
	}

	@Column(name = "toPositionDate")
	public Integer getToPositionDate() {
		return toPositionDate;
	}

	public void setToPositionDate(Integer toPositionDate) {
		this.toPositionDate = toPositionDate;
	}

	@JsonApiToOne
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.JOIN)
	// huibati
	@JoinColumn(name = "privilege_id", foreignKey = @ForeignKey(name = "null"))
	public Privilege getPrivilege() {
		return privilege;
	}

	public void setPrivilege(Privilege privilege) {
		this.privilege = privilege;
	}

	// -----

	@Column(name = "login_name")
	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(final String loginName) {
		this.loginName = loginName;
	}

	@JsonApiToOne
	// @JsonApiIncludeByDefault
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.SELECT)
	// huibati
	@JoinColumn(name = "org_id", foreignKey = @ForeignKey(name = "null"))
	public Organization getOrg() {
		return org;
	}

	public void setOrg(Organization org) {
		this.org = org;
	}

	@JsonApiToOne
	@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.REFRESH)
	@Fetch(FetchMode.SELECT)
	// huibati
	@JoinColumn(name = "role_id", foreignKey = @ForeignKey(name = "null"))
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@Column(name = "name")
	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	@Column(name = "ent_name")
	public String getEntName() {
		return entName;
	}

	public void setEntName(final String entName) {
		this.entName = entName;
	}

	@Transient
	// @Column(name = "password")
	public String getPassword() {
		return null;
	}

	public void setPassword(final String password) {
		this.password = password;
	}

	@Column(name = "avatar")
	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(final String avatar) {
		this.avatar = avatar;
	}

	@Column(name = "descr")
	public String getDesc() {
		return desc;
	}

	public void setDesc(final String desc) {
		this.desc = desc;
	}

	@Column(name = "online_status")
	public Short getOnlineStatus() {
		return onlineStatus;
	}

	public void setOnlineStatus(Short onlineStatus) {
		this.onlineStatus = onlineStatus;
	}

	@Column(name = "status")
	public Short getStatus() {
		return status;
	}

	public void setStatus(final Short status) {
		this.status = status;
	}

	@Column(name = "weixin")
	public String getWeixin() {
		return weixin;
	}

	public void setWeixin(final String weixin) {
		this.weixin = weixin;
	}

	@Column(name = "qqnum")
	public String getQqnum() {
		return qqnum;
	}

	public void setQqnum(String qqnum) {
		this.qqnum = qqnum;
	}

	@Column(name = "curaddress")
	public String getCurAddress() {
		return curAddress;
	}

	public void setCurAddress(final String curAddress) {
		this.curAddress = curAddress;
	}

	@Column(name = "age")
	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	/*
	 * @JsonApiToMany
	 * 
	 * @JsonApiIncludeByDefault
	 * 
	 * @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	 * 
	 * @JoinTable(name = "ccd_service_lable", joinColumns = {
	 * 
	 * @JoinColumn(name = "cs_id", nullable = false, updatable = false,
	 * foreignKey = @ForeignKey(name = "null")) }, inverseJoinColumns = {
	 * 
	 * @JoinColumn(name = "lable_id", nullable = false, updatable = false,
	 * foreignKey = @ForeignKey(name = "null")) })
	 * 
	 * @Fetch(FetchMode.SELECT)//huibati public Set<Dicttype> getCstag() {
	 * return cstag; }
	 * 
	 * public void setCstag(Set<Dicttype> cstag) { this.cstag = cstag; }
	 */

	/*
	 * @JsonApiToMany
	 * 
	 * @JsonApiIncludeByDefault
	 * 
	 * @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	 * 
	 * @JoinTable(name = "ccd_user_language", joinColumns = {
	 * 
	 * @JoinColumn(name = "user_id", nullable = false, updatable = false,
	 * foreignKey = @ForeignKey(name = "null")) }, inverseJoinColumns = {
	 * 
	 * @JoinColumn(name = "language_id", nullable = false, updatable = false,
	 * foreignKey = @ForeignKey(name = "null")) })
	 * 
	 * @Fetch(FetchMode.JOIN) public Set<Dicttype> getLanguage() { return
	 * language; }
	 * 
	 * public void setLanguage(Set<Dicttype> language) { this.language =
	 * language; }
	 */
	@Column(name = "introduce")
	public String getIntroduce() {
		return introduce;
	}

	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}

	/*
	 * @JsonApiToOne
	 * 
	 * @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	 * 
	 * @JoinColumn(name = "extend_info", foreignKey = @ForeignKey(name =
	 * "null"))
	 * 
	 * @Fetch(FetchMode.SELECT)//huibati public Userextend getExtendInfo() {
	 * return extendInfo; }
	 * 
	 * public void setExtendInfo(Userextend extendInfo) { this.extendInfo =
	 * extendInfo; }
	 * 
	 * @JsonApiToOne
	 * 
	 * @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	 * 
	 * @JoinColumn(name = "edinfo", foreignKey = @ForeignKey(name = "null"))
	 * 
	 * @Fetch(FetchMode.SELECT)//huibati public Edinfo getEdinfo() { return
	 * edinfo; }
	 * 
	 * public void setEdinfo(Edinfo edinfo) { this.edinfo = edinfo; }
	 */

	/*
	 * @JsonApiToOne
	 * 
	 * @JsonApiIncludeByDefault
	 * 
	 * @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
	 * 
	 * @JoinColumn(name = "agentinfo",foreignKey = @ForeignKey(name = "null"))
	 * 
	 * @Fetch(FetchMode.JOIN) public UserAgent getAgentinfo() { return
	 * agentinfo; }
	 * 
	 * public void setAgentinfo(UserAgent agentinfo) { this.agentinfo =
	 * agentinfo; }
	 */

	@Column(name = "reg_way")
	public String getRegWay() {
		return regWay;
	}

	public void setRegWay(String regWay) {
		this.regWay = regWay;
	}

	@Column(name = "verify_fail_reason")
	public String getVerifyFailReason() {
		return verifyFailReason;
	}

	public void setVerifyFailReason(String verifyFailReason) {
		this.verifyFailReason = verifyFailReason;
	}

	@Column(name = "inviter")
	public String getInviter() {
		return inviter;
	}

	public void setInviter(String inviter) {
		this.inviter = inviter;
	}

	@JsonApiToOne
	@ManyToOne
	@JoinColumn(name = "invite_user", nullable = true, foreignKey = @ForeignKey(name = "null"))
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonBackReference
	public User getInviteUser() {
		return inviteUser;
	}

	public void setInviteUser(User inviteUser) {
		this.inviteUser = inviteUser;
	}

	@JsonApiToMany
	@OneToMany(mappedBy = "inviteUser")
	@JsonManagedReference
	public List<User> getInvitees() {
		return invitees;
	}

	public void setInvitees(List<User> invitees) {
		this.invitees = invitees;
	}

	@Column(name = "remark")
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Transient
	public String getAuthCode() {
		return authCode;
	}

	public void setAuthCode(String authCode) {
		this.authCode = authCode;
	}
}